package cn.yy.cloudpay.util;

import java.math.BigDecimal;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.text.Html;
import android.text.Spanned;

/**
 * @ClassName: KeyboardStringUtil
 * @Description: TODO 处理自定义数字键盘按键的点击事件
 * @author [shibx]（永易科技）
 * @date 2016年4月26日 下午2:17:57
 */
public class KeyboardStringUtil {
	
	private static boolean HAS_POINT = false; // 键盘是否有小数点

	//用于填充字符串
	private final static String ZERO = "0";
	private final static String DOUBLE_ZERO = "00";
	//原始总金额——货币格式 "￥0.00" 单位为元
	private static String amount = "￥0.00";
	//原始总金额 ——字符串 "1234" 单位为分
	public static String summary = "";
	
	private static String amountString = "";
	
	private static boolean isInputPoint = false;
	private static List<String> integerList = new ArrayList<String>();
	private static List<String> decimalList = new ArrayList<String>();
	
	/**
	 * 数字键点击返回结果方法
	 * 当 summary 为空字符串时，按“0”无效
	 * 当 summary 长度大于11时，按数字键不再增加
	 * @param number 数字键
	 * @return
	 */
//	public static Spanned getNumKeyResult(String number) {
//		StringBuilder builder = new StringBuilder(summary);
//		if (("".equals(summary) && "0".equals(number))|| summary.length() == 9) {
//			builder.append("");
//		} else {
//			builder.append(number);
//		}
//		setSummary(builder.toString());
//		return getDiaplayString(summary);
//	}
	
	/**
	 * 数字键点击返回结果方法
	 * 当 summary 为空字符串时，按“0”无效
	 * 当 summary 长度大于11时，按数字键不再增加
	 * @param number 数字键
	 * @return
	 */
	public static Spanned getNumKeyResult(String number) {
		StringBuilder builder = new StringBuilder(summary);
		if(HAS_POINT){ // 键盘有小数点
			if(".".equals(number)){
				isInputPoint = true;
				return getDiaplayString(summary);
			}
			if (summary.length() == 9) {
				
			} else {
				// 如果已输入小数点
				if(isInputPoint){
					if(decimalList.size() < 2){
						decimalList.add(number);
					}
				}else{ // 输入的是整数部分
					if(integerList.size() == 0 && "0".equals(number)){
						
					}else{
						integerList.add(number);
					}
				}
				builder.delete(0, builder.length());
				getSumBuilder(builder);
			}
		}else{ // 键盘没有小数点
			if(".".equals(number)){
				builder.append("");
			}else if (("".equals(summary) && "0".equals(number))|| summary.length() == 9) {
				builder.append("");
			} else {
				builder.append(number);
			}
		}
		setSummary(builder.toString());
		return getDiaplayString(summary);
	}
	
	private static void getSumBuilder(StringBuilder builder){
		for(String str : integerList){
			builder.append(str);
		}
		for(String str : decimalList){
			builder.append(str);
		}
		for(int i = 0; i < (2-decimalList.size()); i++){
			builder.append("0");
		}
	}

	/**
	 * 设置金额字符串，单位为分
	 * @param summary
	 */
	private static void setSummary(String summary) {
		KeyboardStringUtil.summary = summary;
	}
	
	public static String getSummary() {
		return KeyboardStringUtil.summary;
	}

	/**
	 * 退格键点击返回结果
	 * summary不为空字符串 即退格
	 * @return
	 */
//	public static Spanned getBackSpaceResult() {
//		StringBuilder builder = new StringBuilder(summary);
//		if (builder.length() > 0) {
//			builder.deleteCharAt(builder.length() - 1);
//		}
//		setSummary(builder.toString());
//		return getDiaplayString(summary);
//	}
	
	/**
	 * 退格键点击返回结果（键盘有小数点）
	 * summary不为空字符串 即退格
	 * @return
	 */
	public static Spanned getBackSpaceResult() {
		StringBuilder builder;
		if(HAS_POINT){ // 键盘有小数点
			builder = new StringBuilder("");
			if(decimalList.size() > 0){
				decimalList.remove(decimalList.size() - 1);
			}else {
				isInputPoint = false;
				if(integerList.size() > 0){
					integerList.remove(integerList.size() - 1);
				}
			}
			getSumBuilder(builder);
		}else{ // 键盘没有小数点
			builder = new StringBuilder(summary);
			if (builder.length() > 0) {
				builder.deleteCharAt(builder.length() - 1);
			}
		}
		setSummary(builder.toString());
		return getDiaplayString(summary);
	}

	/**
	 * 重置金额
	 * @return
	 */
	public static Spanned getResetResult() {
		summary = "";
		isInputPoint = false;
		integerList.clear();
		decimalList.clear();
		return getDiaplayString(summary);
	}

	/**
	 * 获取显示的金额字符串
	 * 
	 * @return
	 */
	public static Spanned getDiaplayString(String summary) {
		String result = "";
		String integerStr = ""; // 整数
		String decimalStr = ""; // 小数
		if("".equals(summary)) {
			integerStr = ZERO;
			decimalStr = DOUBLE_ZERO;
		} else if(summary.length() < 3) {
			integerStr = ZERO;
			decimalStr = summary;
			while (decimalStr.length() < 2) {
				decimalStr = ZERO + decimalStr;
			}
		} else if(summary.indexOf(".")!=-1){
			setSummary(changeY2F(summary));
			int i = summary.indexOf(".");
			integerStr = summary.substring(0,i);
			decimalStr = summary.substring(i+1,summary.length());
		} else {
			integerStr = summary.substring(0, summary.length()-2);
			decimalStr = summary.substring(summary.length()-2);
//			setSummary(changeY2F(summary));
		}
		setAmount(integerStr,decimalStr);
		result = "<html>￥<big>" + integerStr + ".</big><small>" + decimalStr
				+ "</small></html>";
		amountString = result;
		return Html.fromHtml(result);
	}

	public static String changeY2F(String amount){
		BigDecimal rs = BigDecimal.valueOf(Double.valueOf(amount)).multiply(new BigDecimal(100));
		Integer v = rs.intValue();
		return v.toString();
	}

	/**
	 * 设置货币格式的金额字符串
	 * @param integerStr 整数
	 * @param decimalStr 小数
	 */
	private static void setAmount (String integerStr,String decimalStr) {
		amount = integerStr + "." + decimalStr;
		// 想要转换成指定国家的货币格式  
		NumberFormat format = NumberFormat.getCurrencyInstance(Locale.CHINA); 
		amount = format.format(Double.parseDouble(amount));
	}
	
	/**
	 * 获取货币格式的金额字符串
	 * @return
	 */
	public static String getAmount() {
		return amount;
	}
	
	/**
	 * 获取html标签格式字符串
	 * @return
	 */
	public static String getAmountHtmlString() {
		return amountString;
	}
}
